#!/bin/sh
set -e


TRIPLETS="/ /i386-linux-gnu/ /x86_64-linux-gnu/ /aarch64-linux-gnu/ /powerpc64le-linux-gnu/"

add_slave()
{
	local target_link name source_path prefix
	target_link="$1"
	name="$2"
	source_path="$3"
	prefix="nvidia--"

	if [ -f "${source_path}" ] && [ -d "$(dirname "${target_link}")" ]; then
		echo --slave "${target_link}" "${prefix}${name}" "${source_path}"
	fi
}

add_multiarch_slave()
{
	local target_dir target_sub_dir file source_dir source_sub_dir prefix suffix triplet
	target_dir="$1"
	target_sub_dir="$2"
	file="$3"
	source_dir="$4"
	source_sub_dir="$5"
	prefix="$6"

	for triplet in $TRIPLETS ; do
		# s|/$||; s|^/|-|;
		suffix="${triplet%/}"
		suffix="${suffix:+-${suffix#/}}"
		add_slave \
			"${target_dir}${triplet}${target_sub_dir}${file}" \
			"${prefix}${file}${suffix}" \
			"${source_dir}${triplet}${source_sub_dir}${file}"
	done
}

# A trigger that handles the alternatives for /usr/lib[/<triplet>]/nvidia/*.*
if [ "$1" = "triggered" ]; then

	slaves="
		$(add_slave /usr/lib/nvidia/libglxserver_nvidia.so libglxserver_nvidia.so /usr/lib/#PRIVATE#/libglxserver_nvidia.so)
		$(add_slave /usr/lib/nvidia/nvidia_drv.so nvidia_drv.so /usr/lib/#PRIVATE#/nvidia_drv.so)
		$(add_multiarch_slave /usr/lib vdpau/ libvdpau_nvidia.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libGLX_nvidia.so.0 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libEGL_nvidia.so.0 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libGLESv1_CM_nvidia.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libGLESv2_nvidia.so.2 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libcuda.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libcuda.so /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libcudadebugger.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvcuvid.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvcuvid.so /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-allocator.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-api.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-encode.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-fbc.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-ml.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-ngx.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-nvvm.so.4 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-nvvm.so.#VERSION# /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-opencl.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-opticalflow.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvidia-ptxjitcompiler.so.1 /usr/lib #PRIVATE#/)
		$(add_multiarch_slave /usr/lib "" libnvoptix.so.1 /usr/lib #PRIVATE#/)
		$(add_slave /usr/share/nvidia/nvoptix.bin nvoptix.bin /#LIBDIR#/#PRIVATE#/nvoptix.bin)
		$(add_multiarch_slave /usr/lib gbm/ nvidia-drm_gbm.so /usr/lib #PRIVATE#/)
		$(add_slave /usr/bin/nvidia-smi nvidia-smi /usr/lib/#PRIVATE#/nvidia-smi)
		$(add_slave /usr/share/man/man1/nvidia-smi.1.gz nvidia-smi.1.gz /usr/lib/#PRIVATE#/nvidia-smi.1.gz)
		$(add_slave /usr/lib/nvidia/nvidia-bug-report.sh nvidia-bug-report.sh /usr/lib/#PRIVATE#/nvidia-bug-report.sh)
		$(add_slave /usr/bin/nvidia-debugdump nvidia-debugdump /usr/lib/#PRIVATE#/nvidia-debugdump)
		$(add_slave /usr/share/nvidia/nvidia-application-profiles-key-documentation nvidia-application-profiles-key-documentation /usr/share/nvidia/nvidia-application-profiles-#VERSION#-key-documentation)
		$(add_slave /usr/bin/nvidia-settings nvidia-settings /usr/lib/#PRIVATE#/nvidia-settings)
		$(add_slave /usr/bin/nv-control-dpy nv-control-dpy /usr/lib/#PRIVATE#/nv-control-dpy)
		$(add_slave /usr/share/applications/nvidia-settings.desktop nvidia-settings.desktop /usr/lib/#PRIVATE#/nvidia-settings.desktop)
		$(add_slave /usr/share/man/man1/nvidia-settings.1.gz nvidia-settings.1.gz /usr/lib/#PRIVATE#/nvidia-settings.1.gz)
"
	conf_slaves="
		$(add_multiarch_slave /usr/lib nvidia/ libnvidia-cfg.so.1 /usr/lib #PRIVATE#/)
		$(add_slave /etc/nvidia/nvidia-drm-outputclass.conf nvidia-drm-outputclass.conf /etc/#PRIVATE#/nvidia-drm-outputclass.conf)
"
	kmod_slaves="
		$(add_slave /etc/nvidia/nvidia-blacklists-nouveau.conf nvidia-blacklists-nouveau.conf /etc/nvidia/#NVIDIA#-#VERSION#/nvidia-blacklists-nouveau.conf)
		$(add_slave /etc/nvidia/nvidia-modprobe.conf nvidia-modprobe.conf /etc/nvidia/#NVIDIA#-#VERSION#/nvidia-modprobe.conf)
		$(add_slave /etc/modprobe.d/nvidia-options.conf nvidia-options.conf /etc/nvidia/#NVIDIA#-#VERSION#/nvidia-options.conf)
		$(add_slave /etc/nvidia/nvidia-load.conf nvidia-load.conf /etc/nvidia/#NVIDIA#-#VERSION#/nvidia-load.conf)
"
	kmod_open_slaves="
		$(add_slave /etc/nvidia/nvidia-blacklists-nouveau.conf nvidia-blacklists-nouveau.conf /etc/nvidia/#VERSION#-open/nvidia-blacklists-nouveau.conf)
		$(add_slave /etc/nvidia/nvidia-modprobe.conf nvidia-modprobe.conf /etc/nvidia/#VERSION#-open/nvidia-modprobe.conf)
		$(add_slave /etc/modprobe.d/nvidia-options.conf nvidia-options.conf /etc/nvidia/#VERSION#-open/nvidia-options.conf)
		$(add_slave /etc/nvidia/nvidia-load.conf nvidia-load.conf /etc/nvidia/#VERSION#-open/nvidia-load.conf)
"
	libnvidia_ml_so_slave=
	if [ -f /usr/include/nvml.h ]; then
		libnvidia_ml_so_slave="$(add_multiarch_slave /usr/lib "" libnvidia-ml.so /usr/lib #PRIVATE#/)"
	fi
	normal_alternative=0
	open_alternative=0
	if echo "$slaves" | grep -q "slave" ; then
		if echo "${kmod_slaves}" | grep -q "slave" ; then
			normal_alternative=1
		fi
		if echo "${kmod_open_slaves}" | grep -q "slave" ; then
			open_alternative=1
		else
			# fallback: normal alternative w/o kernel module
			normal_alternative=1
		fi
	fi
	if [ "$normal_alternative" = 1 ]; then
		update-alternatives --install /usr/lib/nvidia/nvidia nvidia /usr/lib/nvidia/#CURRENT# #MAJOR# $slaves $conf_slaves $kmod_slaves $libnvidia_ml_so_slave
	else
		update-alternatives --remove nvidia /usr/lib/nvidia/#CURRENT#
	fi
	if [ "$open_alternative" = 1 ]; then
		update-alternatives --install /usr/lib/nvidia/nvidia nvidia /usr/lib/nvidia/#CURRENT#-open $((#MAJOR# - 1)) $slaves $conf_slaves $kmod_open_slaves $libnvidia_ml_so_slave
	else
		update-alternatives --remove nvidia /usr/lib/nvidia/#CURRENT#-open
	fi

	# activate the trigger selecting NVIDIA as GLX provider
	dpkg-trigger --no-await register-glx-alternative-nvidia

	# let glx-alternative-mesa take over handling libGLX_indirect.so.0
	dpkg-trigger --no-await register-glx-alternative-mesa

fi


if [ "$1" = "configure" ]; then

	# activate our trigger
	dpkg-trigger register-#NVIDIA#-alternative

fi


#DEBHELPER#
